import json
from pathlib import Path

import requests
from urllib3 import encode_multipart_formdata

# {'maintainDept': '10000000040014', 'roadNo': 'G1', 'strategyParamList': [{'id': '1', 'maintainDept':
# '10000000040014', 'maintainDeptName': None, 'roadNo': 'G1', 'roadName': None, 'startingStation': '102.000',
# 'roadLen': 199.682, 'duration': 3, 'budgetFee': 5800.22, 'trafficLevel': 3, 'inTime': '2022-12-05 17:34:57',
# 'upTime': '2022-12-05 17:34:57', 'endingStation': None, 'lineTotal': 6, 'sectionRoad': None}, {'id': '2',
# 'maintainDept': '10000000040014', 'maintainDeptName': None, 'roadNo': 'G1', 'roadName': None, 'startingStation':
# '41.904', 'roadLen': 21.303, 'duration': 7, 'budgetFee': 3100.0, 'trafficLevel': 3, 'inTime': '2022-12-05
# 17:34:57', 'upTime': '2022-12-09 09:49:27', 'endingStation': None, 'lineTotal': 6, 'sectionRoad': None}]}


# eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpbmZvSWQiOiIxNjI2NDMxNTgwNjM1NDAyMjQyIiwic3lzVHlwZSI6IjEiLCJ1c2VyX25hbWUiOiIxODA5MjIzNDg5NjEiLCJzY29wZSI6WyJ3ZWJhcHAiXSwibmFtZSI6IuadqOS8nyIsImlkIjoiMTYyNjQzMTU4MTM4MTk4ODM1MyIsImV4cCI6MTY3NzEwMDUzMywianRpIjoiN2M2ZDgyMTgtNjNmOC00MGIyLTk1ZDMtMzUyODQ3Y2E0ZjE0IiwiYXV0aG9yaXRpZXMiOltdLCJjbGllbnRfaWQiOiJ3ZWJhcHAiLCJ1c2VybmFtZSI6IjE4MDkyMjM0ODk2MSJ9.SY__BTnLQ5Hf7_AO2K5iyeOOfEa-zMKeu1YMRShbO_o



# eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpbmZvSWQiOiIxNjI2NDMxNTgwNjM1NDAyMjQyIiwic3lzVHlwZSI6IjEiLCJ1c2VyX25hbWUiOiIxODA5MjIzNDg5NjEiLCJzY29wZSI6WyJ3ZWJhcHAiXSwibmFtZSI6IuadqOS8nyIsImlkIjoiMTYyNjQzMTU4MTM4MTk4ODM1MyIsImV4cCI6MTY3NzQ5ODI0NiwianRpIjoiYWNiZmUzNjQtYWViNC00OTgxLThjZmQtOGJkODk4OWMxNjJjIiwiYXV0aG9yaXRpZXMiOltdLCJjbGllbnRfaWQiOiJ3ZWJhcHAiLCJ1c2VybmFtZSI6IjE4MDkyMjM0ODk2MSJ9.c6gd8WCSZ5ETsKXssMtjPHFnP94o4gbBmannMRyBVfc



formdata = {
    'maintainDept': '10000000040014',
    'roadNo': 'G1'
}

# 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NzcwNzk3MDksImlhdCI6MTY3NzA3MjUwOSwic3ViIjoiamluIn0.sy8QB1BzD2RPtobJgoTsNzBuPkKcB8_krBg2HJrsq3M'


url1 = 'http://test.imydao.cn:57020/maintain/outside-strategy-param/queryStrategyParam4TollRoad'
url2 = 'http://test.imydao.cn:57020/maintain/outside-strategy-param/queryStrategyParam4SectionRoad'

headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0',
           'Accept': 'image/avif,image/webp,*/*',
           'Accept-Language': 'en-US,en;q=0.5',
           'Accept-Encoding': 'gzip, deflate',
            'Authorization' : 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpbmZvSWQiOiIxNjI2NDMxNTgwNjM1NDAyMjQyIiwic3lzVHlwZSI6IjEiLCJ1c2VyX25hbWUiOiIxODA5MjIzNDg5NjEiLCJzY29wZSI6WyJ3ZWJhcHAiXSwibmFtZSI6IuadqOS8nyIsImlkIjoiMTYyNjQzMTU4MTM4MTk4ODM1MyIsImV4cCI6MTY3NzQ5ODI0NiwianRpIjoiYWNiZmUzNjQtYWViNC00OTgxLThjZmQtOGJkODk4OWMxNjJjIiwiYXV0aG9yaXRpZXMiOltdLCJjbGllbnRfaWQiOiJ3ZWJhcHAiLCJ1c2VybmFtZSI6IjE4MDkyMjM0ODk2MSJ9.c6gd8WCSZ5ETsKXssMtjPHFnP94o4gbBmannMRyBVfc'
           }


# response = requests.post(url1, json=formdata, headers=headers)
# print(response)
# if response.status_code == 200:
#     print('Get data: {}'.format(response.text))
#     print('Get data: {}'.format(response.json()))
#     print('Get data: {}'.format(response.json()['data'][0]))
#     print('Get data: {}'.format(len(response.json()['data'])))
#


def query_json(maintainDept, roadNo):
    data = {}
    data['maintainDept'] = maintainDept
    data['roadNo'] = roadNo
    print('url: {}'.format(url1))

    resp = requests.post(url1, json=data, headers=headers)
    print('post response: {}'.format(resp))
    print('post message: {}'.format(resp.text))
    if resp.status_code == 200:
        text = resp.json()['data'][0]
        return parse_parameters(text)
    return []

'''
@ parameter 
id
maintainDept
maintainDeptName
roadNo
roadName
startingStation
roadLen
duration
budgetFee
trafficLevel
inTime
upTime
endingStation
lineTotal
sectionRoad
'''


def parse_parameters(text):
    if not 'strategyParamList' in text:
        raise ValueError('{} not  complete ')
    paramList = text['strategyParamList']
    print(paramList, type(paramList))
    print(len(paramList), type(paramList))
    for p in paramList:
        maintainDept = p['maintainDept']
        roadNo = p['roadNo']
        roadLen = p['roadLen']
        duration = p['duration']
        budgetFee = p['budgetFee']
        trafficLevel = p['trafficLevel']

    return paramList


def upload_result(filepath, startPileNo):
    filename = Path(filepath).name
    # url = 'http://test.imydao.cn:57020/maintain/outside-strategy-param/importMaintainStrategyDetail'
    url = 'http://test.imydao.cn:57020/maintain/outside-strategy-param/importMaintainStrategyDetail'
    # with open(filepath, 'rb') as f:
    #     file = {'file': {'filename', f.read()}}
    file_data = {'file': (filename, open(filepath, 'rb').read())}
    encode_data = encode_multipart_formdata(file_data)
    print('encode data: {}'.format(encode_data[0]))
    data = encode_data[0]
    header = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0',
              'Content-Type': encode_data[1],
              'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpbmZvSWQiOiIxNjI2NDMxNTgwNjM1NDAyMjQyIiwic3lzVHlwZSI6IjEiLCJ1c2VyX25hbWUiOiIxODA5MjIzNDg5NjEiLCJzY29wZSI6WyJ3ZWJhcHAiXSwibmFtZSI6IuadqOS8nyIsImlkIjoiMTYyNjQzMTU4MTM4MTk4ODM1MyIsImV4cCI6MTY3NzQ5ODI0NiwianRpIjoiYWNiZmUzNjQtYWViNC00OTgxLThjZmQtOGJkODk4OWMxNjJjIiwiYXV0aG9yaXRpZXMiOltdLCJjbGllbnRfaWQiOiJ3ZWJhcHAiLCJ1c2VybmFtZSI6IjE4MDkyMjM0ODk2MSJ9.c6gd8WCSZ5ETsKXssMtjPHFnP94o4gbBmannMRyBVfc'
              }
    res = requests.post(url, headers=header, data=data)
    print('Upload result: {}'.format(res))
    print('message: {}'.format(res.text))
    return res


if __name__ == '__main__':
    query_json('10000000040014', 'G1')
